package com.perfect.service;

import com.perfect.base.BaseService;
import com.perfect.dao.ReasonMapper;
import com.perfect.utils.AssertUtil;
import com.perfect.vo.Reason;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author YU
 */
@Service
public class ReasonService extends BaseService<Reason, Integer> {

    @Resource
    private ReasonMapper reasonMapper;

    /**
     * 更新原因类型
     * @param reason
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateReason(Reason reason){
        AssertUtil.isTrue(checkParamsIsBlank(reason.getId()),"类型Id为空");
        AssertUtil.isTrue(selectByPrimaryKey(reason.getId()) == null,"找不到目标记录");
        AssertUtil.isTrue(reasonMapper.updateByPrimaryKeySelective(reason) < 1 ,"更新失败");
    }

    /**
     * 添加原因类型
     * @param reason
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveReason(Reason reason){
        reason.setIsAvailable("1");
        AssertUtil.isTrue(insertSelective(reason) < 1,"原因类型添加失败");
    }

    /**
     * 根据id删除退货原因记录
     * @param id
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteReasonByID(Integer id){
        AssertUtil.isTrue(checkParamsIsBlank(id),"缺少参数");
        AssertUtil.isTrue(selectByPrimaryKey(id) == null,"没有相应记录");
        AssertUtil.isTrue(deleteByPrimaryKey(id) < 1,"删除失败");
    }

    /**
     * 参数的非空判断
     * @param params
     * @return
     */
    public boolean checkParamsIsBlank(Object ...params){
        for (Object o : params){
            if (o == null){
                return true;
            }
        }
        return false;
    }
}
